package com.yxy.controller;

import com.yxy.common.PageResult;
import com.yxy.common.Result;
import com.yxy.pojo.dto.*;
import com.yxy.pojo.vo.RateVO;
import com.yxy.pojo.vo.WantOrSeeVO;
import com.yxy.properties.JwtProperties;
import com.yxy.service.RateService;
import com.yxy.service.RemarkService;
import com.yxy.service.WantOrSeeService;
import com.yxy.utils.JwtUtils;
import com.yxy.utils.QiniuOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/bfbbs")
@Slf4j
public class CommonController {

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private RemarkService remarkService;
    @Autowired
    private RateService rateService;
    @Autowired
    private WantOrSeeService wantOrSeeService;

    /**
     * 文件上传 腾讯云
     * @param file
     * @return
     */
//    @PostMapping("/upload")
//    public Result<String> upload(MultipartFile file){
//        log.info("文件名：{}", file.getOriginalFilename());
//        // 调用封装的上传到腾讯云对象存储的方法来获取最终的文件名
//        String uploadFileName = TencentCOSUtil.upLoadFile(file);
//        return Result.success("上传成功", uploadFileName);
//    }

    /**
     * 文件上传 七牛云
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件名：{}", file.getOriginalFilename());
        String uploadFileName = QiniuOSSUtil.uploadToQiniu(file);
        return Result.success("上传成功", uploadFileName);
    }

    /**
     * 校验token是否过期
     *
     * @param token
     * @return
     */
    @GetMapping("/checkToken")
    public Result checkToken(@RequestParam String token) {
        try {
            JwtUtils.parseJwt(jwtProperties.getSignKey(), token);
            log.info("checkToken：token有效");
        } catch (Exception e) {
            // 令牌已过期
            log.info("checkToken：toke过期");
            return Result.error(401, "expired");
        }
        return Result.success("", null);
    }

    /**
     * 写短评
     *
     * @return
     */
    @PostMapping("/writeReviews")
    public Result writeReviews(@RequestBody RemarkDTO remarkDTO) {
        log.info("评论信息：{}", remarkDTO);
        remarkService.writeReviews(remarkDTO);
        return Result.success("评论成功", null);
    }

    /**
     * 获取短评
     *
     * @param pageRemarkQueryDTO（get请求不需要@RequestBody）
     * @return
     */
    @GetMapping("/getReviews")
    public Result<PageResult> getReviews(PageRemarkQueryDTO pageRemarkQueryDTO) {
        log.info("分页获取评论信息：{}", pageRemarkQueryDTO);
        PageResult pageResult = remarkService.pageRemarkQuery(pageRemarkQueryDTO);
        return Result.success(24, "", pageResult);
    }

    /**
     * 用户评分
     *
     * @param rateDTO
     * @return
     */
    @PostMapping("/rate")
    public Result rate(@RequestBody RateDTO rateDTO) {
        log.info("用户评分：{}", rateDTO);
        rateService.rate(rateDTO);
        return Result.success(24, "success", null);
    }

    /**
     * 获取评分
     *
     * @return
     */
    @GetMapping("/getRate")
    public Result<RateVO> getRate(TypeDTO typeDTO) {
        log.info("获取评分：{}", typeDTO);
        RateVO vo = rateService.getRate(typeDTO);
        return Result.success(24, "", vo);
    }

    /**
     * 获取想看或看过的状态
     * @param typeDTO
     * @return
     */
    @GetMapping("/getWantOrSeeStatus")
    public Result<WantOrSeeVO> getWantOrSeeStatus(TypeDTO typeDTO){
        log.info("获取想看或看过的状态:{}", typeDTO);
        WantOrSeeVO vo = wantOrSeeService.getWantOrSeeStatus(typeDTO);
        return Result.success(24, "", vo);
    }

    /**
     * 获取用户想看或看过的数据
     * @param pageWantOrSeeQueryDTO
     * @return
     */
    @GetMapping("/getWantOrSeeList")
    public Result<PageResult> getWantOrSeeList(PageWantOrSeeQueryDTO pageWantOrSeeQueryDTO){
        log.info("获取用户想看或看过的数据：{}", pageWantOrSeeQueryDTO);
        PageResult pageResult = wantOrSeeService.getUserWantOrSee(pageWantOrSeeQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 想看或看过
     *
     * @return
     */
    @PostMapping("/wantOrSee")
    public Result wantOrSee(@RequestBody WantOrSeeDTO wantOrSeeDTO) {
        log.info("修改想看或想看的状态：{}", wantOrSeeDTO);
        wantOrSeeService.updateStatus(wantOrSeeDTO);
        return Result.success("", null);
    }
}
